<?php 
$your_email ='kristof.beckers@ktalanaken.be';// <<=== update to your email address

session_start();
$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';

$username1 = $_SESSION['myusername'];
$username = explode(".",$username1);
$leerling = strtoupper($username[1]). " " .strtoupper($username[2]);

include ("../include/config.php");
require_once ("../include/db.php");

if(isset($_POST['submit']))
{
	
	$name = $_POST['name'];
	$klas = $_POST['klas'];
	$pwd = $_POST['pwd'];
	$visitor_email = $_POST['email'];
	$user_message = $_POST['message'];
	///------------Do Validations-------------
	if(empty($name)||empty($visitor_email)||empty($klas)||empty($pwd))
	{
		$errors .= "\n Naam, email, klas en wachtwoord zijn verplichte velden. ";	
	}
	if(IsInjected($visitor_email))
	{
		$errors .= "\n Dit is geen geldig email adres!";
	}
	if(empty($_SESSION['6_letters_code'] ) ||
	  strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
	{
	//Note: the captcha code is compared case insensitively.
	//if you want case sensitive match, update the check above to
	// strcmp()
		$errors .= "\n De captcha code komt niet overeen!";
	}
	
	if(empty($errors))
	{
		//send the email
		$to = $your_email;
		$subject="Support request voor de ouderavond";
		$from = $visitor_email;
		$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		
		$body = "$name heeft een support ticket ingestuurd:\n\n".
		"Naam: $name\n".
		"Email: $visitor_email \n".
		"Klas: $klas \n".
		"Wachtwoord: $pwd \n".
		"Boodschap: \n ".
		"$user_message\n\n".
		"IP: $ip\n";	
		
		$headers = "From: $from \r\n";
		$headers .= "Reply-To: $visitor_email \r\n";
		
		mail($to, $subject, $body,$headers);
		
		header('Location: ../index.php');
	}
}

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<head>
<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,900" rel="stylesheet" />
<link href="<?php echo $workurl ; ?>templates/alicebourg/default.css" rel="stylesheet" type="text/css" media="all" />
<link href="<?php echo $workurl ; ?>templates/alicebourg/fonts.css" rel="stylesheet" type="text/css" media="all" />
</head>

<body>
<div id="wrapper">
	<div id="header-wrapper">
    	<div id="header" class="container">
        	<div id="logo">
            <h1><?php echo $title; ?></h1>
            </div>
        </div>
        <div id="menu" class="container">
        <?php include ("../include/menu.php"); ?>
       </div>

     </div>
     <div id="page" class="container">
     <div id="sidebar1">
     <div id="box1">
     <?php if (isset($_SESSION['myusername']))
	 {
		 ?>
     <h2> Leerling</h2>
     <?php echo $leerling; ?> <br />
     <?php echo "Klas: ".$_SESSION['klas']; ?><br /><br />
     <?php  } ?>
     Wanneer u problemen ondervindt bij het inloggen of het maken van een afspraak, kan u via dit formulier uw gegevens doorsturen en onze ICT dienst zal zo snel mogelijk contact met u opnemen.
	 
     </div>
     </div>
     <div id="content">
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id='contact_form_errorloc' class='err'></div>
<form method="POST" name="contact_form" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> 
<p>
<label for='Naam'>Naam: </label><br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
<p>
<label for='message'>Klas:</label> <br>
<input type="text" name="klas" value='<?php echo htmlentities($name_klas) ?>'>
</p>
<p>
<label for='message'>Wachtwoord:</label> <br>
<input type="text" name="pwd" value='<?php echo htmlentities($name_pwd) ?>'>
</p>
<p>
<label for='email'>Email: </label><br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>
<p>
<label for='message'>Boodschap:</label> <br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
<p>
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>code:</label><br>
<input id="6_letters_code" name="6_letters_code" type="text"><br>
<small>Kan je de afbeelding niet lezen? Klik  <a href='javascript: refreshCaptcha();'>hier</a> om opnieuw te laden.</small>
</p>
<input type="submit" value="Submit" class='button' name='submit'>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("contact_form");
//remove the following two lines if you like error message box popups
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();

frmvalidator.addValidation("name","req","Vul aub de naam van de leerling in."); 
frmvalidator.addValidation("email","req","Vul aub een email adres op waar we u kunnen bereiken."); 
frmvalidator.addValidation("email","email","Geef een geldig email adres op."); 
frmvalidator.addValidation("klas","klas","Geef de klas op waar uw zoon/dochter zit."); 
frmvalidator.addValidation("pwd","pwd","Geef het wachtwoord in waarmee u probeert in te loggen."); 
</script>
<script language='JavaScript' type='text/javascript'>
function refreshCaptcha()
{
	var img = document.images['captchaimg'];
	img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
}
</script>
</div>
</div>
</div>
</body>
</html>